package cn.shop.dys.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.bean.AddressInfo;
import cn.shop.dys.fragment.BaseFragment;
import cn.shop.dys.fragment.UserDetailsAddressAddFragment;
import cn.shop.dys.fragment.UserDetailsAddressFragment;
import cn.shop.dys.fragment.UserDetailsAddressQueryFragment;
import cn.shop.dys.fragment.UserDetailsNameFragment;
import cn.shop.lib_common.CommonUtil;
import cn.shop.dys.R;
import cn.shop.dys.fragment.UserDetailsFragment;
import cn.shop.dys.widget.NoRightScroller;
import cn.shop.dys.widget.NoScrollViewPager;
/**
 *
 * 用户详情
 * Created by sunjian on 2016/4/18.
 *
 */
// TODO: 2016/4/21 地址联动未做

public class UserDetailsActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, BaseFragment.OnUserDetailsClickListener {
    @ViewInject(R.id.iv_common_back)
    private ImageView mImgDetailsBack;
    @ViewInject(R.id.tv_common_index_titile)
    private TextView mTvDetailsIndexTitle;
    @ViewInject(R.id.iv_common_settings)
    private ImageView mImgDetailsSettings;
    @ViewInject(R.id.nsvp_details)
    private NoScrollViewPager mVpDetails;
    private List<BaseFragment> mFragments=new ArrayList<>();
    private PopupWindow mPopupWindow;
    private int mIndex;
    private UserDetailsAddressQueryFragment addressQueryFragment;
    // private UserInfo mUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_details);
     //   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        ViewUtils.inject(this);
        init();
    }
/**
 * 初始化操作
 * 作者:孙建
 * created at 2016/4/21 19:13
 */
    private void init() {
    //    mPositions.add(0);
        initData();
        initFragment();
        initEvent();
        initViewPager();
        initPagerIndex();
    }
    /**
     * 初始化请求索引
     * 作者:孙建
     * created at 2016/4/21 19:14
     */
    private void initData() {
        Intent intent = getIntent();
    //    mUserInfo = intent.getParcelableExtra("UserInfo");
        mIndex = intent.getIntExtra("index", -1);
    }
/**
 * 初始化vp页码
 * 作者:孙建
 * created at 2016/4/21 19:14
 */
    private void initPagerIndex() {
        int index=mIndex;
        if(index <0){
            index =0;
        }
        mVpDetails.setCurrentItem(index);
    }
   /* public UserInfo getUserInfo(){
        return mUserInfo;
    }*/
/**
 * 初始化fragment
 * 作者:孙建
 * created at 2016/4/21 19:14
 */
    private void initFragment() {
        UserDetailsFragment userDetailsFragment = new UserDetailsFragment();
        userDetailsFragment.setOnUserDetailsClickListener(this);
        mFragments.add(userDetailsFragment);
        mFragments.add(new UserDetailsNameFragment());
        UserDetailsAddressFragment addressFragment = new UserDetailsAddressFragment();
        addressFragment.setOnUserDetailsClickListener(this);
        mFragments.add(addressFragment);
        mFragments.add(new UserDetailsAddressAddFragment());
        addressQueryFragment = new UserDetailsAddressQueryFragment();
        mFragments.add(addressQueryFragment);
        addressFragment.setOnAddressInfoTransferListener(new UserDetailsAddressFragment.OnAddressInfoTransferListener() {
            @Override
            public void transferAddressInfo(AddressInfo addressInfo) {
                addressQueryFragment.setAddressInfo(addressInfo);
            }
        });
    }

    private void initEvent() {
        mImgDetailsBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*if (mPosition != 0) {
                    mVpDetails.setCurrentItem(mPrePosition);
                } else {
                    finish();
                }*/
                disposeBackEvent();
            }
        });
        mImgDetailsSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPopupWindow(v);
            }
        });
        mVpDetails.addOnPageChangeListener(this);
    }
/**
 * 返回键处理
 * 作者:孙建
 * created at 2016/4/21 19:15
 */
    @Override
    public void onBackPressed() {
        disposeBackEvent();
    }

    public void disposeBackEvent() {
        int size = mPositions.size();
        if(size !=0){
            Integer integer = mPositions.get(size - 1);
            mVpDetails.setCurrentItem(integer);
            mPositions.remove(integer);
        }else {
            finish();
        }
    }
/**
 * pw处理
 * 作者:孙建
 * created at 2016/4/21 19:15
 */
    private void initPopupWindow (View view){
        View pwView = View.inflate(this, R.layout.view_popupwindow, null);
        View pwTop=pwView.findViewById(R.id.ll_pw_top);
        View pwBottom=pwView.findViewById(R.id.ll_pw_bottom);
        mPopupWindow = new PopupWindow(pwView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
        int[] location = new int[2];
        view.getLocationInWindow(location);
        mPopupWindow.showAsDropDown(view, (int) -CommonUtil.dpToPx(this, 100), (int) -CommonUtil.dpToPx(this, 0));
        pwTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserDetailsActivity.this, MessageActivity.class);
                startActivity(intent);
                mPopupWindow.dismiss();
            }
        });
        pwBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserDetailsActivity.this, MainActivity2.class);
                startActivity(intent);
                mPopupWindow.dismiss();
            }
        });
        //  popupWindow.showAtLocation(view, Gravity.RIGHT| Gravity.TOP, location[0]+40, location[1] + 40);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVpDetails.removeOnPageChangeListener(this);
    }
/**
 * 初始化vp
 * 作者:孙建
 * created at 2016/4/21 19:15
 */
    private void initViewPager() {
        Field mScroller=null;
        try {
            mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            NoRightScroller scroller=new NoRightScroller(mVpDetails.getContext());
            mScroller.set(mVpDetails,scroller);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        mVpDetails.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

        });
    }
/**
 * 处理vp滑动事件
 * 作者:孙建
 * created at 2016/4/21 19:15
 */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
    private int mPrePosition;
    private int mPosition;
    List<Integer> mPositions=new ArrayList<>();
    @Override
    public void onPageSelected(int position) {
      //  Log.d("test","mPrePosition"+this.mPrePosition);
        this.mPrePosition=this.mPosition;
        this.mPosition=position;
        if(this.mPrePosition<this.mPosition){
            mPositions.add(this.mPrePosition);
      //      Log.d("test", "mIndex" + mIndex);
            if(mIndex>0&&this.mPrePosition==0){
                mPositions.remove(this.mPrePosition);
            }
        }
     //   Log.d("test","position"+position);
        switch (position){
            case 0:
                mTvDetailsIndexTitle.setText("我的资料");
                break;
            case 1:
                mTvDetailsIndexTitle.setText("设置昵称");
                break;
            case 2:
                mTvDetailsIndexTitle.setText("收货地址管理");
                break;
            case 3:
                mTvDetailsIndexTitle.setText("收货地址管理");
                break;
            case 4:
                mTvDetailsIndexTitle.setText("收货地址管理");
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(int index) {
        switch (index) {
            case 0:
                mVpDetails.setCurrentItem(0);
                break;
            case 1:
                mVpDetails.setCurrentItem(1);
                break;
            case 2:
                mVpDetails.setCurrentItem(2);
                break;
            case 3:
                mVpDetails.setCurrentItem(3);
                break;
            case 4:
                mVpDetails.setCurrentItem(4);
                break;
        }
    }
   /* @Override
    public void onBackPressed() {
        int currentItem = mVpDetails.getCurrentItem();
        FragmentBackPressed fragment = (FragmentBackPressed) mFragments
                .get(currentItem);
        fragment.onBackPressed();
    }
    public interface FragmentBackPressed {
        *//**
         * 碎片的返回事件
         *//*
        void onBackPressed();
    }
    @Override
    public void onBackPressed() {
        getActivity().finish();
    }*/
}
